﻿using System;
using System.Web.Script.Serialization;
using System.Xml.Linq;
using System.Collections;
using System.Collections.Generic;
//
using PerceptiveMCAPI.Types.Internal;
//
namespace PerceptiveMCAPI.Types
{
	public class templateInfoOutput : Api_BaseOutput
	{
		public templateInfoResults result { get; set; }
		public templateInfoParms inputParms { get; private set; }
		// --------------------------------------------------------
		public templateInfoOutput( templateInfoInput input )
			: base( input )
		{
			inputParms = input.parms;
			result = new templateInfoResults();
		}
		// --------------------------------------------------- tostring
		public override string ToString()
		{
			return "Not implemented";
		}

		#region ================================================================== format setup

		// ---- serial
		internal void Format( string api_Request, string api_Response )
		{
			this.api_Request = api_Request;
			this.api_Response = api_Response;
			// ----------------------------------
			if ( this.api_OutputType == EnumValues.OutputType.JSON )
				outputJSON();
			else if ( this.api_OutputType == EnumValues.OutputType.XML )
				outputXML();
		}

		// ---- xmlrpc
		internal void Format( x_templateInfo rpcResult )
		{
			this.api_Request = EnumValues.NotSupplied;
			this.api_Response = EnumValues.NotSupplied;
			// ------------------------
			this.result = FormatResults( rpcResult );
		}

		#endregion

		#region  ===================================================================== output result

		// ---------------------------------------------------------- outputXML
		private void outputXML()
		{
			XElement xml = XElement.Parse( this.api_Response );
			Api_Error error = apiSerial.GetXmlOutputError( xml );
			if ( error != null )
			{
				this.api_ErrorMessages.Add( error );
				return;
			}
			// ------------------------------------ get values
			try
			{
				templateInfoResults results = new templateInfoResults();
				//
				IEnumerable<XElement> contents = xml.Element( "default_content" ).Descendants();
				foreach ( XElement cont in contents )
				{
					results.content.Add( cont.Name.LocalName, cont.Value );
				}
				//
				IEnumerable<XElement> sections = xml.Element( "sections" ).Elements( "struct" );
				foreach ( XElement sect in sections )
				{
					results.sections.Add( sect.Value );
				}
				//
				results.source = xml.Element( "source" ).Value;
				results.preview = xml.Element( "preview" ).Value;
				//
				this.result = results;
			}
			// ----------------------------- error
			catch ( Exception ex )
			{
				this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
			}
		}
		// ---------------------------------------------------------- outputJSON
		private void outputJSON()
		{
			JavaScriptSerializer jss = new JavaScriptSerializer();

			try
			{
				if ( this.api_Response.Contains( EnumValues.JSONErrorString ) )
				{
					Api_Error err = jss.Deserialize<Api_Error>( this.api_Response );
					this.api_ErrorMessages.Add( err );
				}
				else
				{
					x_templateInfo jsonResult = jss.Deserialize<x_templateInfo>( this.api_Response );
					this.result = FormatResults( jsonResult );
				}
			}
			catch ( Exception ex )
			{
				this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
			}
		}
		// ---------------------------------------------------------- FormatResults
		private templateInfoResults FormatResults( x_templateInfo x_results )
		{
			templateInfoResults results = new templateInfoResults();
			//
			IDictionaryEnumerator de = x_results.default_content.GetEnumerator();
			while ( de.MoveNext() )
			{
				results.content.Add( de.Key.ToString(), de.Value.ToString() );
			}
			//
			results.sections = apiHelper.MakeListFromArray<string>( x_results.sections );
			results.source = x_results.source;
			results.preview = x_results.preview;
			//
			return results;
		}

		#endregion
	}
}
